package springboot.file.demo;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.*;
import com.alibaba.excel.util.BooleanUtils;
import com.alibaba.excel.util.MapUtils;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import springboot.file.demo.easyexcel.pojo.*;
import springboot.file.demo.easyexcel.serviceImpl.DemoDataServiceImpl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@SpringBootTest
class SpringbootFileDemoApplicationTests2 {

    public String path = "E:\\workSpace\\idea\\testWork\\springboot-demo\\springboot-file-demo\\src\\main\\resources\\excel";
    public String fillPath = "E:\\workSpace\\idea\\testWork\\springboot-demo\\springboot-file-demo\\src\\main\\resources\\fill";

    public String fileName = path + "/" + System.currentTimeMillis() + ".xlsx";
    public String templateFileName = fillPath +"/"+ "simple.xlsx";

    @Autowired
    private DemoDataServiceImpl demoDataService;

    /**
     * 最简单的填充
     *
     * @since 2.1.1
     */
    @Test
    public void simpleFill() {
        // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
        // 方案1 根据对象填充
        // 这里 会填充到第一个sheet， 然后文件流会自动关闭
        FillData fillData = new FillData();
        fillData.setName("张三");
        fillData.setNumber(5.2);
        EasyExcel.write(fileName).withTemplate(templateFileName).sheet("模板").doFill(fillData);

        // 方案2 根据Map填充
//        // 这里 会填充到第一个sheet， 然后文件流会自动关闭
//        Map<String, Object> map = MapUtils.newHashMap();
//        map.put("name", "张三");
//        map.put("number", 5.2);
//        EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(map);
    }
}
